<style>
    .detailHide {
        border: none;
    }

    .deviceForm label {
        width: 120px;
    }

    .companyFormPhotos {
        /* margin-left:140px; */
        line-height: 38px;
        float: left;
        margin-right: 30px;
    }

    .companyFormPhotos img {
        cursor: pointer;
        height: 38px;
        width: auto;
        max-width: 200px;
    }
</style>


<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form lay-filter="addQualificationForm" id="addQualificationForm" class="layui-form model-form">
                <input name="id" type="hidden"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">企业案例名称</label>
                    <div class="layui-input-inline">
                        <input name="name" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">客户企业名称</label>
                    <div class="layui-input-inline">
                        <input name="companyName"  type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">统一信用编码</label>
                    <div class="layui-input-inline">
                        <input name="creditCode"  type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标题图片</label>
                    <div class="layui-input-block">
                        <div id="devicePic_path" class="companyFormPhotos"></div>
                        <div id="devicePicBtn" class="layui-btn" style="float: left;"><i class="layui-icon">&#xe681;</i>上传图片
                        </div>
                        <input name="titleUrl" type="hidden" class="layui-input" lay-verify="" id="titleUrl"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">关键词</label>
                    <div class="layui-input-inline">
                        <input name="keyWords"  type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">一句话简介</label>
                    <div class="layui-input-inline">
                        <input name="remark" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                    </div>
                </div>
                <!--
                        <div class="layui-form-item">
                            <label class="layui-form-label">发布时间</label>
                            <div class="layui-input-inline">
                                <input name="createTime" placeholder="" type="text" class="layui-input" lay-verify="required" required/>
                            </div>
                        </div>
                -->


                <div class="layui-form-item">
                    <label class="layui-form-label">发布时间</label>
                    <div class="layui-input-inline">
                        <!--                <input name="startTime" placeholder="" type="text" class="layui-input date-icon" lay-verify="required" required id="startTime"/>-->
                        <input name="createTime" class="layui-input date-icon" style="width: 200px;" type="text"
                               placeholder="选择时间" autocomplete="off"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">案例描述</label>
                    <div class="layui-input-inline" style="width: 600px">
                        <textarea id="describes" name="describes" placeholder="请输入" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item text-left" style="margin-left: 130px">
<!--                    <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>-->
                    <button class="layui-btn" lay-filter="qualificationFormSubmit" lay-submit>保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
    .layui-form-label {
        width: 100px;
    }
</style>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', "laydate", "layedit", "upload"], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var layedit = layui.layedit;

        /*
                // 渲染表格
                var insTb = table.render({
                    elem: '#qualificationTable',
                    url: config.base + 'enterprise/qualification/admin/qualification/list',
                    page: true,
                    cellMinWidth: 100,
                    cols: [[
                        {type: 'checkbox'}
                        , {field: 'id', sort: true, title: 'ID'}
                        , {field: 'name', sort: true, title: '资质名称'}
                        , {field: 'certification', sort: true, title: '认证机构'}
                        , {field: 'remark', sort: true, title: '描述'}
                        , {field: 'statusDesc', sort: true, title: '状态'}
                        , {field: 'createTime', sort: true, title: '添加时间'}
                        /!*
                                        ,{field: 'qualificationTypeId', sort: true, title: '资质类型'}
                                        ,{field: 'qualificationUrl', sort: true, title: '资质照片'}
                                        ,{field: 'qualificationDescribe', sort: true, title: '资质描述'}
                                        ,{field: 'stopTime', sort: true, title: '资质有效终止期'}
                                        ,{field: 'startTime', sort: true, title: '资质有效开始期'}*!/
                        , {align: 'center', toolbar: '#qualificationTableBar', title: '操作', width: 200}
                    ]],
                    done: function (res, curr, count) {
                        //移除按钮
                        var menuButton = config.getMenuButton(location.hash);
                        if (menuButton.indexOf("搜索") == -1) {
                            $('.layui-form.toolbar').remove();
                        }
                        if (menuButton.indexOf("修改") == -1) {
                            $("[lay-event='edit']").remove();
                        }
                        if (menuButton.indexOf("删除") == -1) {
                            $("[lay-event='del']").remove();
                        }
                    }
                });

                // 工具条点击事件
                table.on('tool(qualificationTable)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'detail') { //修改
                        showDetailModel(data);
                    } else if (obj.event === 'del') { // 删除
                        doDelete(obj);
                    }
                });

                //监听排序
                table.on('sort(qualificationTable)', function (obj) {
                    table.reload('qualificationTable', {
                        initSort: obj
                        , where: {
                            sort: obj.field
                            , order: obj.type
                        }
                    });
                });

                // 搜索
                form.on('submit(formSubSearchLog)', function (data) {
                    insTb.reload({where: data.field, page: {curr: 1}}, 'data');
                });


                // 显示编辑弹窗
                function showDetailModel(data) {
                    admin.open({
                        type: 1,
                        area: ['1180px', '800px'],
                        offset: '65px',
                        title: '资质详情',
                        content: $('#qualificationForm').html(),
                        success: function () {
                            form.val('qualificationForm', data);
                            admin.req('enterprise/qualification/admin/qualification/getQualificationById', {id: data.id}, function (res) {
                                console.log(res.data)

                                $('#qualificationUrl').html('<img src="' + res.data.qualificationUrl + '" style="height: 35px;" />');
                            }, 'GET', false)
                        }
                    });
                }

                $('#qualificationBtnAdd').click(function () {
                    showAddModel();
                });*/

        // admin.open({
        //     type: 1,
        //     area: ['1180px', '800px'],
        //     offset: '65px',
        //     title: '添加资质',
        //     content: $('#addQualificationForm').html(),
        //     success: function () {
        //         // form.val('addQualificationForm', data);
        //         // 表单提交事件
        //         form.on('submit(qualificationFormSubmit)', function (d) {
        //
        //             d.field.qualificationDescribe = $('#qualificationDescribe').val()
        //             layer.load(2);
        //             admin.req('enterprise/qualification/admin/qualification/add', d.field, function (res) {
        //                 layer.closeAll('loading');
        //                 if (res.code == 200) {
        //                     layer.msg(res.msg, {icon: 1});
        //                     table.reload('qualificationTable');
        //                     layer.closeAll('page');
        //                 } else {
        //                     layer.msg(res.msg, {icon: 2});
        //                 }
        //             }, 'POST');
        //             return false;
        //         });
        //     }
        // });

        form.on('submit(qualificationFormSubmit)', function (d) {

            d.field.describes = $('#describes').val()
            layer.load(2);
            admin.req('enterprise/addCustomerCase/admin/customerCase/add', d.field, function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    //清空表单数据
                    $("#addQualificationForm")[0].reset();
                    $('#devicePic_path').html('')
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
            return false;
        });
        //时间范围
        laydate.render({
            elem: 'input[name="startTime"]',
            type: "datetime",
            format: 'yyyy-MM-dd HH:mm:ss'
        });
        laydate.render({
            elem: 'input[name="stopTime"]',
            type: "datetime",
            format: 'yyyy-MM-dd HH:mm:ss'
        });
        laydate.render({
            elem: 'input[name="createTime"]',
            type: "datetime",
            format: 'yyyy-MM-dd HH:mm:ss'
        });
        //上传头像
        $('#devicePicBtn').click(uploadImg('devicePicBtn', 'devicePic_path', 'titleUrl'))

        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].productType}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 3) {
                    html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                } else if (type == 4) {
                    html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if (type == 5) {
                    html += `<option value='${data[i].skuCode}'>${data[i].skuName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

        //下拉框 渲染
        admin.req('enterprise/qualification/admin/qualificationType/getQualificationType', {}, function (res) {
            if (res.code == 200) {
                loadSelectHtml(res.data, "选择资质类型", "qualificationTypeId", 2);
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');


        // 上传文件
        function uploadImg(_elem, pathId, inputId) {
            upload.render({
                elem: '#' + _elem
                , url: config.upload_url + 'uploadFile'
                , data: {}
                , accept: 'file'
                , exts: 'jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx'
                , headers: {Authorization: 'Bearer ' + config.getToken()}
                , done: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg || "上传成功", {icon: 1});
                        let imagUrl = res.data.url;
                        console.log(imagUrl)
                        $('#' + pathId).html('<img src="' + imagUrl + '"/>');
                        $('#' + inputId).val(imagUrl);
                    } else if (res.code == 502) {
                        return location.replace('admin/login.html');
                    } else {
                        layer.msg(res.msg || "上传失败", {icon: 2});
                    }
                }
            });
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/qualification/admin/qualification/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        //批量删除
        $('#batchDel').click(function () {
            //获取选中的数据
            var checkRows = table.checkStatus('qualificationTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
            } else {
                var ids = '';
                checkRows.data.forEach(item => {
                    ids += item.id + ','
                })
                delBatch(ids)
            }
        });

        function delBatch(ids) {

            admin.req('enterprise/qualification/admin/qualification/delBatch', {ids: ids}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('qualificationTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 导出excel
        $('#qualificationBtnExport').click(function () {
            var checkRows = table.checkStatus('qualificationTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>